package com.fivefu.core.hik.services.impl;

import com.alibaba.fastjson.JSON;
import com.fivefu.base.common.utils.str.StrUtils;
import com.fivefu.core.hik.constant.HikRequestTypeEnum;
import com.fivefu.core.hik.dto.request.PageReq;
import com.fivefu.core.hik.dto.request.VideoNetworkReq;
import com.fivefu.core.hik.dto.response.ResponsePage;
import com.fivefu.core.hik.dto.response.ResponseResult;
import com.fivefu.core.hik.services.DbHikCameraService;
import com.fivefu.core.hik.services.HikvisionService;
import com.fivefu.core.hik.services.VideoNetWorkService;
import com.fivefu.core.hik.vo.CameraOnlienResponseVo;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Service
public class VideoNetWorkServiceImpl implements VideoNetWorkService {

    @Resource
    private DbHikCameraService dbHikCameraService;

    @Resource
    private HikvisionService hikvisionService;

    @Override
    public void queryVideoIntegrity(String beginTime, String endTime) {
        Integer pageNo = 1;
        Integer PageSize = 1000;
        getVideoIntegrity(pageNo,PageSize,beginTime,endTime);
    }

    @Override
    public void queryVideoQuality() {
        Integer pageNo = 1;
        Integer PageSize = 500;
        getVideoQuality(pageNo,PageSize);
    }

    @Override
    public void queryOnlineCamera() {
        Integer pageNo = 1;
        Integer PageSize = 1000;
        getOnlineCamera(pageNo,PageSize);
    }

    @Override
    public void queryOnlineEncodeDevice() {
        Integer pageNo = 1;
        Integer PageSize = 1000;
        getOnlineEncodeDevice(pageNo,PageSize);
    }

    /**
     * 获取编码设备在线状态
     * @param pageNo 当前页
     * @param pageSize 每页显示条数
     */
    private void getOnlineEncodeDevice(Integer pageNo, Integer pageSize) {
        PageReq req = new PageReq(pageNo, pageSize);
        String body = JSON.toJSONString(req);
        //查询枚举
        HikRequestTypeEnum typeEnum = HikRequestTypeEnum.NMSENCODEDEVICE;
        //调用接口
        ResponseResult result = hikvisionService.doPostString(typeEnum.getUrl(), body, null, null, typeEnum.getType());
        if (result != null && "0".equals(result.getCode())){//调用成功
            String resultData = result.getData().toString();
            if (StrUtils.isNotNull(resultData)){
                ResponsePage resultPage = JSON.parseObject(resultData, ResponsePage.class);

                if (resultPage.getTotal() > ((pageNo-1)*pageSize + pageSize)){
                    getOnlineEncodeDevice(pageNo+1,pageSize);
                }
            }
        }
    }

    /**
     * 获取监控点在线状态
     * @param pageNo 当前页
     * @param pageSize 每页显示条数
     */
    private void getOnlineCamera(Integer pageNo, Integer pageSize) {
        PageReq req = new PageReq(pageNo, pageSize);
        String body = JSON.toJSONString(req);
        //查询枚举
        HikRequestTypeEnum typeEnum = HikRequestTypeEnum.NMSCAMERA;
        //调用接口
        ResponseResult result = hikvisionService.doPostString(typeEnum.getUrl(), body, null, null, typeEnum.getType());
        if (result != null && "0".equals(result.getCode())){//调用成功
            String resultData = result.getData().toString();
            if (StrUtils.isNotNull(resultData)){
                ResponsePage resultPage = JSON.parseObject(resultData, ResponsePage.class);

                resultPage.getList().forEach(obj -> {
                    CameraOnlienResponseVo resp = JSON.parseObject(JSON.toJSONString(obj),CameraOnlienResponseVo.class);
                    Integer onlineStatus = 0;
                    if (resp.getOnline() != null){
                        if (resp.getOnline().equals(0)){
                            onlineStatus = 2;
                        }else if (resp.getOnline().equals(1)){
                            onlineStatus = 1;
                        }
                    }
                    //编辑监控点信息
                    dbHikCameraService.changeCameraOnlineStatus(resp.getIndexCode(), onlineStatus);
                });


                if (resultPage.getTotal()>((pageNo-1)*pageSize + pageSize)){
                    getOnlineCamera(pageNo+1,pageSize);
                }
            }
        }
    }

    /**
     * 根据监控点列表查询视频质量诊断结果
     * @param pageNo 当前页
     * @param pageSize 每页显示条数
     */
    private void getVideoQuality(Integer pageNo, Integer pageSize) {
        //获得监控点唯一标识
        List<String> ids = dbHikCameraService.queryCameraIds();
        for (String indexCode : ids) {
            List<String> list = new ArrayList<>();
            list.add(indexCode);
            VideoNetworkReq req = new VideoNetworkReq(pageNo, pageSize, list);
            String body = JSON.toJSONString(req);
            //查询枚举
            HikRequestTypeEnum typeEnum = HikRequestTypeEnum.NMSVQD;
            //调用接口
            ResponseResult result = hikvisionService.doPostString(typeEnum.getUrl(), body, null, null, typeEnum.getType());
            if (result !=null && "0".equals(result.getCode())){//调用成功
                String resultData = result.getData().toString();
                if (StrUtils.isNotNull(resultData)){
                    ResponsePage resultPage = JSON.parseObject(resultData, ResponsePage.class);

                }
            }

        }
    }

    /**
     * 根据监控点列表查询录像完整性结果
     * @param pageNo 当前页
     * @param pageSize 每页显示条数
     * @param beginTime 录像开始时间
     * @param endTime 录像结束时间
     */
    private void getVideoIntegrity(Integer pageNo, Integer pageSize, String beginTime, String endTime) {
        //获得监控点唯一标识
        List<String> ids = dbHikCameraService.queryCameraIds();
        for (String indexCode : ids) {
            List<String> list = new ArrayList<>();
            list.add(indexCode);
            VideoNetworkReq req = new VideoNetworkReq(pageNo, pageSize, beginTime, endTime, list);
            String body = JSON.toJSONString(req);

            //查询枚举
            HikRequestTypeEnum typeEnum = HikRequestTypeEnum.NMSRECORD;
            //调用接口
            ResponseResult result = hikvisionService.doPostString(typeEnum.getUrl(), body, null, null, typeEnum.getType());
            if (result !=null && "0".equals(result.getCode())){//调用成功
                String resultData = result.getData().toString();
                if (StrUtils.isNotNull(resultData)){
                    ResponsePage resultPage = JSON.parseObject(resultData, ResponsePage.class);
                    /*resultPage.getList().forEach(o -> {

                    });*/

                }
            }
        }
    }
}
